<template>
<div>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form v-loading="formLoading" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
     <el-form-item label="活动名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="活动名称"></el-input>
    </el-form-item>
    <el-form-item label="农事类型" prop="typeDict">
      <!-- <el-input v-model="dataForm.typeDict" placeholder="农事类型"></el-input> -->
       <el-select style="width:100%" v-model="dataForm.typeDict" filterable placeholder="请选择农事类型">
            <el-option
            v-for="item in nslx"
            :key="item.id"
            :label="item.name"
            :value="item.id">
            </el-option>
        </el-select>
      
    </el-form-item>

    <el-form-item label="所在田块" prop="farmlandInfoId">
        <el-select  style="width:100%" v-model="dataForm.farmlandInfoId" filterable placeholder="请输入关键字">
            <el-option
            v-for="item in farmList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
            </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="负责人" prop="handler">
        <el-select  style="width:100%" v-model="dataForm.handler" filterable placeholder="请选择负责人">
            <el-option
            v-for="item in fzr"
            :key="item.id"
            :label="item.name"
            :value="item.id">
            </el-option>
        </el-select>
    </el-form-item>
   
    <el-form-item label="活动内容" prop="remark">
      <el-input :rows="3" type="textarea" v-model="dataForm.remark" placeholder="活动内容"></el-input>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-loading="formLoading" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
   
    </div>
</template>

<script>
import { treeDataTranslate } from '@/utils'
import { debug } from 'util';
  export default {
    data () {
      return {
        visible: false,
        formLoading: false,
         demoVisible: false,
        dataForm: {
          id: 0,
          typeDict: '',
          handler: '',
          deptId: '',
          parentName:'',
          farmlandInfoId: '',
          name: '',
          remark: '',
          createId: '',
          createName: '',
          createTime: '',
          updateId: '',
          updateTime: '',
          updateName: '',
          version: '',
          gcFlag: ''
        },
        dataRule: {
          typeDict: [
            { required: true, message: '农事类型不能为空', trigger: 'blur' }
          ],
          handler: [
            { required: true, message: '负责人不能为空', trigger: 'blur' }
          ],
          farmlandInfoId: [
            { required: true, message: '田块不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '活动名称不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '活动内容不能为空', trigger: 'blur' }
          ],

        },
         menuList: [],
         multipleSelection: [],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        key: '',
        menuListTreeProps: {
          label: 'name',
          children: 'children'
        },
        farmList:[],//
        fzr:[],// 负责人
        nslx:[]
      }
    },
    methods: {
       init (id) {
         // 
        this.$http({
          url: this.$http.adornUrl('/buss/bussfarmlandinfo/list'),
          method: 'get',
          params: this.$http.adornParams({
              // "type": "crop_00"
          })
        }).then(({data}) => {
          debugger;
          this.farmList = treeDataTranslate(data.page.list, 'id')
          console.log(this.farmList);

        })
         // 获取农事类型字典列表
        this.$http({
          url: this.$http.adornUrl('/sys/dict/select'),
          method: 'get',
          params: this.$http.adornParams({
               "type": "farm_act_00"
          })
        }).then(({data}) => {
          debugger;
           this.nslx = treeDataTranslate(data.list, 'id')
          console.log(this.farmList);

        })

         this.$http({
          url: this.$http.adornUrl('/sys/user/list2'),
          method: 'get',
          params: this.$http.adornParams({
              // "type": "crop_00"
          })
        }).then(({data}) => {
          debugger;
          this.fzr = treeDataTranslate(data.page.list, 'id')
          console.log(this.fzr);

        })

        this.dataForm.id = id || 0
        this.$http({
          url: this.$http.adornUrl('/sys/dept/list'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {

          this.menuList = treeDataTranslate(data, 'id')
        }).then(() => {
        this.visible = true
        this.formLoading = false
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
           })
          }).then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/buss/bussfarmingactivity/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                //debugger;
                this.dataForm.typeDict = data.bussFarmingActivity.typeDict
                this.dataForm.handler = data.bussFarmingActivity.handler
                this.dataForm.deptId = data.bussFarmingActivity.deptId
                this.dataForm.farmlandInfoId = data.bussFarmingActivity.farmlandInfoId
                this.dataForm.name = data.bussFarmingActivity.name
                this.dataForm.remark = data.bussFarmingActivity.remark
                this.dataForm.createId = data.bussFarmingActivity.createId
                this.dataForm.createName = data.bussFarmingActivity.createName
                this.dataForm.createTime = data.bussFarmingActivity.createTime
                this.dataForm.updateId = data.bussFarmingActivity.updateId
                this.dataForm.updateTime = data.bussFarmingActivity.updateTime
                this.dataForm.updateName = data.bussFarmingActivity.updateName
                this.dataForm.version = data.bussFarmingActivity.version
                this.dataForm.gcFlag = data.bussFarmingActivity.gcFlag
                 this.menuListTreeSetCurrentNode();
              }
            })
          }
        })
      },
       changeInput() {
        this.demoVisible = true
         if (this.multipleSelection.length == 0) {
           this.search()
         }
      },
      handleSelectionChange(val) {
        // console.log('%c------------ 验证是否每次都调用', 'color:green')
        // console.log(val)
        // this.multipleSelection = val
        // let list = []
        // val.forEach(x => {
        //   list.push(x.id)
        // })
        // this.dataForm.farmlandInfoIds = list.join(',')

        console.log( this.dataList);
         this.dataList.forEach(item => {
           console.log('当前id-'+item.id+'----之后的id--'+val.id);
          if (item.id !== val.id) {
            item.checked = false
          }
        })
        this.dataForm.farmlandInfoIds = val.id;

      },
       search() {
        this.$http({
          url: this.$http.adornUrl('/buss/bussfarmlandinfo/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.key,
            'jd':this.dataForm.deptId
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            if (this.multipleSelection.length > 0) {
              this.multipleSelection.forEach(x => {
                data.page.list.forEach((y, index) => {
                    if (x.id == y.id) {
                      this.$refs.multipleTable.toggleRowSelection(data.page.list[index], true)
                    }
                })
              })
            }
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
       // 菜单树选中
      menuListTreeCurrentChangeHandle (data, node) {
      debugger;
        this.dataForm.deptId = data.id
        this.dataForm.parentName = data.name
      },
      // 菜单树设置当前选中节点
      menuListTreeSetCurrentNode () {

        this.$refs.menuListTree.setCurrentKey(this.dataForm.deptId)
        this.dataForm.parentName = (this.$refs.menuListTree.getCurrentNode() || {})['name']
      },
      // 图标选中
      iconActiveHandle (iconName) {
        this.dataForm.icon = iconName
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid && !this.formLoading) {
            this.formLoading = true
            this.$http({
              url: this.$http.adornUrl(`/buss/bussfarmingactivity/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'typeDict': this.dataForm.typeDict,
                'handler': this.dataForm.handler,
                'deptId': this.dataForm.deptId,
                'farmlandInfoId': this.dataForm.farmlandInfoId,
                'name': this.dataForm.name,
                'remark': this.dataForm.remark,
                'createId': this.dataForm.createId,
                'createName': this.dataForm.createName,
                'createTime': this.dataForm.createTime,
                'updateId': this.dataForm.updateId,
                'updateTime': this.dataForm.updateTime,
                'updateName': this.dataForm.updateName,
                'version': this.dataForm.version,
                'gcFlag': this.dataForm.gcFlag
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.formLoading = false
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
